package buildcode.support.plugin;

import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;

import buildcode.Context;
import buildcode.build.IPlugin;
import buildcode.config.EntityExportConfig;
import buildcode.database.DataBaseTable;
import buildcode.language.Type;
import buildcode.util.CommonUtil;

public class CommonExtraDataPlugin implements IPlugin {

    @Override
    public Type beforeExport(Type type, String contextId, List<DataBaseTable> bases, EntityExportConfig entityConfig) {
        if (bases.size() == 1) {
            DataBaseTable base = bases.get(0);
            String entityName = base.getCamelCaseName();
            if (StringUtils.isNotBlank(Context.getTablePrfix()) && base.getTableName().startsWith(Context.getTablePrfix())) {
                entityName = entityName.replace(CommonUtil.firstUpper(Context.getTablePrfix()), "");
            }
            type.getExtraData().put("simpleEntityName", entityName);
            type.setRemark(Optional.ofNullable(base.getTableRemark()).map(r -> r.replace("表", "")).orElse(""));
        }
        return type;
    }

    public CommonExtraDataPlugin() {
    }

}
